package main

import (
	"fmt"
	"sort"
)

// 1636. 按照频率将数组升序排序
// 给你一个整数数组 nums ，请你将数组按照每个值的频率 升序 排序。如果有多个值的频率相同，请你按照数值本身将它们 降序 排序。

// 请你返回排序后的数组。

func frequencySort(nums []int) []int {
	freq := make(map[int]int)
	for i := range nums {
		freq[nums[i]]++
	}
	numsWithFreq := [][]int{}
	for k, v := range freq {
		elem := []int{k, v}
		numsWithFreq = append(numsWithFreq, elem)
	}
	sort.Slice(numsWithFreq, func(i, j int) bool {
		return numsWithFreq[i][1] < numsWithFreq[j][1] || numsWithFreq[i][1] == numsWithFreq[j][1] && numsWithFreq[i][0] > numsWithFreq[j][0]

	})

	res := []int{}
	for i := range numsWithFreq {
		for j := 0; j < numsWithFreq[i][1]; j++ {
			res = append(res, numsWithFreq[i][0])
		}
	}
	return res
}

func main() {
	nums := []int{1, 1, 2, 2, 2, 3}
	res := frequencySort(nums)
	fmt.Println(res)
}
